﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using myBoard.Framework.MVVM;

namespace myTwitter.BO.Search
{
    public class Entry : IMessageBase
    {
        Random _random = new Random();

        public string Id { get; set; }
        public DateTime Published { get; set; }
        public string LinkUrl { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public DateTime Updated { get; set; }
        public string ImageUrl { get; set; }
        public string TwitterSource { get; set; }
        public string TwitterLanguage { get; set; }
        public Author Author { get; set; }
        private User _user;

        #region IMessage Members

        public override DateTime CreatedAt
        {
            get
            {
                return Published;
            }
            set
            {
                Published = value;
            }
        }

        public override long ID
        {
            get
            {
                return _random.Next(0, int.MaxValue);
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override string Text
        {
            get
            {
                return Content;
            }
            set
            {
                Content = value;
                RaisePropertyChanged("Text");
            }
        }

        public override User User
        {
            get
            {
                if (_user == null)
                {
                    string authorName = Author.Name;
                    if (authorName.Contains('(') && authorName.Contains(')'))
                    {
                        authorName = authorName.Remove(0, authorName.IndexOf('(') + 1);
                        authorName = authorName.Replace(")", "");
                    }

                    _user = new User()
                    {
                        Name = authorName,
                        ScreenName = Author.Uri.Replace("http://twitter.com/", ""),
                        ProfileImageUrl = ImageUrl
                    };                    
                }
                
                return _user;
            }
            set
            {
                _user = value;
            }
        }

        #endregion
    }
}
